import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt
from numpy.lib.function_base import gradient#Matplotlib是RGB

img = cv2.imread('dige.png')

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

#礼帽与黑帽
#礼帽：原始输入-开运算结果
#黑帽：闭运算-原始输入

kernel = np.ones((7,7),np.uint8) 
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)

cv2.imshow('tophat', tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()

#黑帽
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('blackhat', blackhat)
cv2.waitKey(0)
cv2.destroyAllWindows()